package flc.ast.activity;

import static com.hanks.passcodeview.PasscodeView.PasscodeViewType.TYPE_SET_PASSCODE;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.DeviceAdapter;
import flc.ast.adapter.QuestionAdapter;
import flc.ast.bean.MyQuestionBean;
import flc.ast.databinding.ActivitySetPwdBinding;
import flc.ast.utils.Content;
import flc.ast.view.PasscodeView;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.view.container.StkLinearLayout;
import stark.common.basic.view.container.StkRecycleView;

public class SetPwdActivity extends BaseAc<ActivitySetPwdBinding> {

    private int selectMenuIndex = 0;
    private String[] dataList;
    private List<MyQuestionBean> listQuestion=new ArrayList<>();
    private Dialog myQuestionDialog;
    private StkRecycleView dialogQuestionList;
    private QuestionAdapter questionAdapter;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.activity_set_pwd;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSetPwdBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.llQuestion.setOnClickListener(this);
        mDataBinding.ivSetAnswerNext.setOnClickListener(this);
        mDataBinding.passcodeView.setListener(new PasscodeView.PasscodeViewListener() {
            @Override
            public void onFail() {
                mDataBinding.passcodeView.getWrongStatusColor();
            }

            @Override
            public void onSuccess(String number) {
                SPUtil.putString(mContext, Content.myPassword, number);
                mDataBinding.llSetSecure.setVisibility(View.VISIBLE);
                mDataBinding.llSetPwd.setVisibility(View.GONE);
            }
        });
        mDataBinding.etSetAnswerText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String str=s.toString();
                if (str.isEmpty()){
                    mDataBinding.ivSetAnswerNext.setImageResource(R.drawable.ashezhiwancwei);
                }else {
                    mDataBinding.ivSetAnswerNext.setImageResource(R.drawable.ashezhiwanc);
                }
            }
        });
        //密保问题弹窗
        questionDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.llQuestion:
                myQuestionDialog.show();
                break;
            case R.id.ivSetAnswerNext:
                String answer = mDataBinding.etSetAnswerText.getText().toString().trim();
                String problem = mDataBinding.tvSelAnswerProblem.getText().toString();
                if (answer.isEmpty()){
                    return;
                }
                SPUtil.putString(mContext, Content.myAnswer, answer);
                SPUtil.putString(mContext, Content.myProblem, problem);
                SPUtil.putBoolean(mContext, Content.isHavePassword, true);
                ToastUtils.showShort(R.string.password_set_suc);
                startActivity(EncipherVideoActivity.class);
                finish();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        questionAdapter.getItem(oldPosition).setSelect(false);
        questionAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        questionAdapter.getItem(position).setSelect(true);
        mDataBinding.tvSelAnswerProblem.setText(questionAdapter.getItem(position).getQuestion());
        questionAdapter.notifyItemChanged(position);
    }

    @Override
    protected void initData() {
        dataList = getResources().getStringArray(R.array.question_list);
        for (int i = 0; i < dataList.length; i++) {
            listQuestion.add(new MyQuestionBean(dataList[i],false));
        }
        listQuestion.get(0).setSelect(true);
        questionAdapter.setList(listQuestion);
    }

    //密保问题弹窗
    private void questionDialog() {
        myQuestionDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_question, null);
        myQuestionDialog.setContentView(inflate);
        myQuestionDialog.setCancelable(true);
        Window dialogWindow = myQuestionDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        dialogQuestionList = inflate.findViewById(R.id.rvDialogQuestionList);
        dialogQuestionList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        questionAdapter = new QuestionAdapter();
        dialogQuestionList.setAdapter(questionAdapter);
        questionAdapter.setOnItemClickListener(this);
    }

}